import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "@/stores";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      // 登录
      path: "/login",
      name: "login",
      component: () => import("@/views/login/LoginPage.vue"),
    },

    {
      //布局架子
      path: "/",
      component: () => import("@/views/layout/LayoutContainer.vue"),
      redirect: "/home",
      children: [
        {
          // 首页
          path: "/home",
          component: () => import("@/views/homepage/Home.vue"),
        },
        {
          // 资讯信息
          path: "/info",
          component: () => import("@/views/information/Information.vue"),
        },
        {
          // 热门农产品
          path: "/farmProducts",
          component: () => import("@/views/products/FarmProducts.vue"),
        },
        {
          // 我的订单
          path: "/myOrder",
          component: () => import("@/views/userCenter/MyOrder.vue"),
        },
        {
          // 农户申请
          path: "/farmerApply",
          component: () => import("@/views/farmerCenter/FarmerApply.vue"),
        },
        {
          // 上传农产品
          path: "/uploadProducts",
          component: () => import("@/views/farmerCenter/UploadProducts.vue"),
        },
      ],
    },
    {
      //布局架子 管理员
      path: "/admin",
      component: () => import("@/views/layout/LayoutContainer2.vue"),
      // 重定向到扶贫政策
      redirect: "/policy",
      children: [
        {
          // 扶贫政策
          path: "/policy",
          component: () => import("@/views/admin/Information/Policy.vue"),
        },
        {
          // 用户管理
          path: "/user",
          component: () => import("@/views/admin/userManage/UserManage.vue"),
        },
        {
          // 农户申请管理
          path: "/apply",
          component: () =>
            import("@/views/admin/farmerApplication/FarmerApplication.vue"),
        },
        {
          // 农产品类型管理
          path: "/productType",
          component: () =>
            import("@/views/admin/productManage/ProductType.vue"),
        },
        {
          // 农产品管理
          path: "/product",
          component: () => import("@/views/admin/productManage/Product.vue"),
        },
        {
          // 订单管理
          path: "/order",
          component: () => import("@/views/admin/order/Order.vue"),
        },
        {
          // 个人基本资料
          path: "/profile",
          component: () =>
            import("@/views/admin/userCenter/UpdateUserInfo.vue"),
        },
        {
          // 修改密码
          path: "/updatePassword",
          component: () =>
            import("@/views/admin/userCenter/UpdatePassword.vue"),
        },
      ],
    },
  ],
});

// 路由守卫
router.beforeEach((to) => {
  const useStore = useUserStore();
  // 未登录状态
  if (!useStore.token) {
    // 1. 访问登录页 → 正常放行
    if (to.path === "/login") {
      return true;
    }
    // 2. 访问首页 → 正常放行（避免循环重定向）
    if (
      to.path === "/home" ||
      to.path === "/info" ||
      to.path === "/farmProducts"
    ) {
      return true;
    }
    // 3. 访问其他页面（如/admin下的路由）→ 拦截到登录页
    return "/login";
  }
  // 已登录状态 → 正常放行
  return true;
});

export default router;
